<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/18 0018
 * Time: 上午 10:03
 */
namespace  app\admin\controller;
use app\common\controller\Curt;
use think\Validate;
use think\Db;
use phpExcel\phpexcel;

class Rider extends Curt {
    function __construct()
    {
        parent::__construct($upload = true,$uploadConfig = array(
            'input_name' => 'coverFile',//文本框name
            'data_field' => 'cover'//数据库字段
        ));
    }
    public function getall()
    {
        $keyword=trim(input('get.keyword'));
        $where['status']=1;
        if($keyword){
            $where['realname|phone|username']=['like','%'.$keyword.'%'];//模糊查询or可以一个条件中写多个字段以‘|’分开
        }
        $list = $this->model->where($where)->order('id desc')->paginate();
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
    }

    /**
     * 添加商户
     */
    public function adds(){
        $data=$this->request->param();
        $rule = [
            'username'=>['require','unique:rider','max:10','min:4'],//登录名
        ];
        $msg = [
            'username.require'   => '名称必须填写！',
            'username.min'   => '名称最短四位!',
            'username.max'   => '名称最长十位!',
            'username.unique'   => '名称已存在！',
        ];
        $validate   = Validate::make($rule,$msg);
        $result = $validate->check($data);
        if(!$result) {
            $this->error($validate->getError());
        }
        $data['shop_pwd']=md5($data['shop_pwd']);
        $data['shop_status']=1;
        $res=$this->model->allowField(true)->save($data);
        if ($res) {
            $this->success('骑手添加成功！', $this->fetchconfig['add_success']);
        } else {
            $this->success('数据异常！', $this->fetchconfig['add_success']);
        }
    }
    /**
     * @return \think\response\Json
     * 重置密码
     */
    public function refresspwd(){
        $data['id']=input('id');
        $data['password']=md5('a123456');
        $this->model->isUpdate(true)->save($data);
        $datas['state']=1;
        $datas['msg']='密码重置成功';
        return  json($datas);
    }
    /**
     * @return \think\response\Json
     * 开启关闭状态
     */
    public function editstatus(){
        $where['id']=input('id');
        $status=input('rider_status');
        if($status==1){
            $status=2;
        }else{
            $status=1;
        }
        $where['rider_status']=$status;

        $this->model->isUpdate(true)->save($where);
        if($status==2){
            $res['msg']="用户已停用！";
        }else{
            $res['msg']="用户已开启！";
        }
        return json($res);
    }

    /**
     * @return mixed
     * 导出骑手的订单信息
     */
    public function statistics(){
        $name =input('name');
        $type = input('type');
        $where['realname']=['like','%'.$name.'%'];
        $where['status']=1;
        $list = $this->model->where($where)->paginate();
        $page=$list->render();
        if (!empty($type)){
            $ExeclModel=new phpexcel();
            $xlsName  = "订单列表信息";
            $xlsCell  = array(
                array("realname",'骑手名称'),
                array("ordernum",'订单数量'),
                array('money','订单总额'),
            );
            $ExeclModel->exportExcel($xlsName,$xlsCell,$list);
        }
        $this->assign('name', $name);
        $this->assign('page', $page);
        $this->assign('riderlist', $this->model->field('realname')->select());
        $this->assign('list', $list);
        return $this->fetch();
    }
}